<?php
declare (strict_types = 1);

namespace app\subscribe;

use app\common\GptService;
use app\model\TalentComment;
use app\model\TalentDocument;
use app\model\UsersRelated;
use think\facade\Log;

class Talent
{
    protected string $eventPrefix = 'Talent';

    /**
     * 增加浏览人数
     * @param $id
     * @return bool
     */
    function onIncBrowse($id): bool
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->inc('browse')->update();
        return true;
    }

    /**
     * 评论增加点赞
     * @param $id
     * @return true
     */
    function onCommentIncLike($id)
    {
        $model = new TalentComment();
        $model->where('id',$id)->inc('like')->update();
        return true;
    }

    /**
     * 评论减少点赞
     * @param $id
     * @return true
     */
    function onCommentDecLike($id)
    {
        $model = new TalentComment();
        $model->where('id',$id)->dec('like')->update();
        return true;
    }

    /**
     * 增加评论
     * @param $id
     * @return true
     */
    function onIncComment($id)
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->inc('comment')->update();
        return true;
    }

    /**
     * 增加点赞
     * @param $id
     * @return true
     */
    function onIncLike($id)
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->inc('like')->update();
        return true;
    }

    /**
     * 减少点赞
     * @param $id
     * @return true
     */
    function onDecLike($id)
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->dec('like')->update();
        return true;
    }

    /**
     * 增加收藏
     * @param $id
     * @return true
     */
    function onIncCollect($id): bool
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->inc('collect')->update();
        return true;
    }


    /**
     * 减少收藏
     * @param $id
     * @return true
     */
    function onDecCollect($id): bool
    {
        $model = new \app\model\Talent();
        $model->where('id',$id)->dec('collect')->update();
        return true;
    }

    
    function onAiDoc($id)
    {
        $row = TalentDocument::find($id);
        $trade=$row->trade?'行业:'.$row->trade:'';
        $job=$row->job?'职业:'.$row->job:'';
        $mark=$row->mark?'其他补充:'.$row->mark:'';
        $content=$trade.$job.$mark;
        switch ($row->type){
            case 1:
                $msg="请根据以下内容生成中文word格式的工作证明".$content;
                break;
            case 2:
                $msg="请根据以下内容生成中文word格式的推荐信".$content;
                break;
            case 3:
                $msg="请根据以下内容生成中文word格式的计划书".$content;
                break;
            default:
                return true;
        }
        $res=GptService::ask($msg);
        $res=json_decode($res);
        if ($res->code!=200){
            $row->status=2;
            $row->reason='生成失败';
            return true;
        }else{
            $key=$res->data->choices[0]->message->content;
            $file_url=public_path().'static/doc/' .$row->name;
            file_put_contents($file_url,$key);
            $link ='/static/doc/'.$row->name;
            UsersRelated::create(['users_id'=>$row->users_id,'pro_id'=>$row->id,'type'=>6,'scene'=>'Talent']);//通知用户
            $row->link=$link;
            $row->status=1;
        }
        $row->save();
        return true;
    }



}
